package com.aiolos.springboot.stepbystep.service.impl;

import com.aiolos.springboot.stepbystep.model.AyMood;
import com.aiolos.springboot.stepbystep.mq.AyMoodProducer;
import com.aiolos.springboot.stepbystep.repository.AyMoodRepository;
import com.aiolos.springboot.stepbystep.service.IAyMoodService;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.Destination;

@Service
public class AyMoodServiceImpl implements IAyMoodService {

    @Resource
    private AyMoodRepository ayMoodRepository;

    //异步保存说说的队列名称
    private static Destination destination = new ActiveMQQueue("ay.queue.asyn.save");

    @Resource
    private AyMoodProducer ayMoodProducer;

    /**
     * 在实现类AyMoodServiceImpl中，我们注入AyMoodRepository接口，
     * 并调用其提供的save() 方法，保存说说到数据库。
     * @param ayMood
     * @return
     */
    @Override
    public AyMood save(AyMood ayMood) {
        return ayMoodRepository.save(ayMood);
    }

    /**
     * 异步保存说说
     * asynSave方法并不保存说说记录，而是调用AyMoodProducer类的sendMessage推送消息
     * @param ayMood
     * @return
     */
    @Override
    public String asynSave(AyMood ayMood) {
        //往队列ay.queue.asyn.save中推送消息，消息内容为说说实体
        ayMoodProducer.sendMessage(destination,ayMood);
        return "success";
    }
}
